import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import postCssPxToRem from "postcss-pxtorem";
import esbuild from "rollup-plugin-esbuild";

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: "/",
    plugins: [
      vue(),
      esbuild({
        target: "chrome64",
        loaders: {
          ".vue": "js",
          ".ts": "js",
        },
      }),
    ],
    build: {
      minify: "terser",
      target: ["edge90", "chrome90", "firefox90", "safari15"], // 适配低版本浏览器
    },
    server: {
      host: "0.0.0.0",
      port: 5555,
      hmr: true,
      proxy: {
        "/dev-api": {
          target: "http://localhost:8904/",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/dev-api/, ""),
        },
      },
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
      },
    },
    css: {
      postcss: {
        plugins: [
          postCssPxToRem({
            rootValue: 16, // 1rem的大小(设计稿宽度的1/10)
            propList: ["*"], // 需要转换的属性，这里选择全部都进行转换
          }),
        ],
      },
    },
  };
});
